/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.scuecsz.view.mbean.common;

import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;
import org.scuecsz.model.bussiness.RegisterLoginModel;
import org.scuecsz.model.to.USER_MAIN;
import org.scuecsz.utils.FileUtil;
import org.scuecsz.view.utils.FaceUtil;
import org.scuecsz.view.utils.ImageData;

/**
 *
 * @author Administrator
 */
public class RegisterMbean extends DefaultMbean {

    private USER_MAIN userMain = new USER_MAIN();
    private ImageData imgData;

    /** Creates a new instance of RegisterMBean */
    public RegisterMbean() {
    }

    public void uploadListener(UploadEvent event) throws Exception {
        UploadItem item = event.getUploadItem();
        imgData.setName(item.getFileName());
        imgData.setLength(item.getFileSize());
        imgData.setData(FileUtil.getBytesFromFile(item.getFile()));
        FaceUtil.setSessionAttribute("IMAGE", imgData);
    }

    public String registAction() {
        try{
            if(1==RegisterLoginModel.regist(userMain,imgData))
              return "next";
        }catch(SQLException e){
            e.printStackTrace();
            System.out.println(e.getErrorCode()==1026);
        }catch(Exception e){

        }
        return null;
    }

    public USER_MAIN getUserMain() {
        return userMain;
    }

    public void setUserMain(USER_MAIN userMain) {
        this.userMain = userMain;
    }

    public void clearAction() {
        imgData.clear();
    }

    public ImageData getImgData() {
        if (null == imgData) {
            imgData = new ImageData();
        }
        return imgData;
    }

    public void setImgData(ImageData imgData) {
        this.imgData = imgData;
    }

    public  void paint(OutputStream stream, Object object) throws IOException {
        ImageData imgDataTemp=(ImageData) FaceUtil.getSessionAttributeValueThenRemove("IMAGE");
        stream.write(imgDataTemp.getData());
        stream.flush();
        stream.close();
    }
}
